<template>
  <div class="app-container">
    <div>
      <el-row :gutter="10">
        <el-col :span="6" :xs="24" :lg="24">
          <div class="bgwhite plr20 ptb20 radius10 flex-col">
            <div class="justify-between w100p">
              <div class="clr3c4 bold ft26">新增</div>
              <div class="clr9fa align-center pointer" @click="back()">
                <el-icon>
                  <Back />
                </el-icon>
                返回上一頁
              </div>
            </div>
            <div class="mt40">
              <el-form :label-position="labelPosition" label-width="auto" :model="formLabelAlign" style="max-width: 600px">
                <el-form-item label="用戶名:" required>
                  <el-input v-model="formLabelAlign.name" style="width: 380px;" placeholder="請輸入用戶名" />
                </el-form-item>
                <el-form-item label="昵稱:" required>
                  <el-input v-model="formLabelAlign.username" placeholder="請輸入昵稱" />
                </el-form-item>
                <el-form-item label="頭像:" style="align-items: flex-start;font-weight: bold;" required>
                  <el-upload v-model:file-list="fileList" :action="$upUrl" list-type="picture-card" :headers="headers" name="images" :on-success="onSuccess" :on-preview="handlePictureCardPreview" :on-remove="handleRemove">
                    <div class="uploader flex-cen" v-if="fileList.length == 0">
                      <el-icon size="60" style="color: #3a3f63;">
                        <Picture />
                      </el-icon>
                    </div>
                    <el-button class="btn2 ml20">選擇文件</el-button>
                  </el-upload>
                </el-form-item>
                <el-form-item label="密碼:" required>
                  <el-input type="password" v-model="formLabelAlign.password" placeholder="請輸入密碼" />
                </el-form-item>
                <el-form-item label="確認密碼:" required>
                  <el-input type="password" v-model="formLabelAlign.check_password" placeholder="請輸入密碼" />
                </el-form-item>
                <el-form-item label="角色:">

                  <el-select v-model="role_id" placeholder="請選擇角色" size="large" style="width: 240px" class="mr10 mb10">
                    <el-option v-for="item in options" :key="item.id" :label="item.name" :value="item.id" />
                  </el-select>
                </el-form-item>
              </el-form>
              <div class="mt40 mb15 flex-cen ">
                <div class="align-center">
                  <el-button class="btn1" @click="reset()">重置</el-button>
                  <el-button class="btn2" @click="submit()">提交</el-button>
                </div>

              </div>
            </div>


          </div>
        </el-col>


      </el-row>
    </div>
  </div>
</template>

<script setup lang="ts">
import { reactive, ref, onMounted } from 'vue'
import { useRouter } from 'vue-router';
import type { FormProps } from 'element-plus'
import { adminAdd, roleList } from '@/api/systemAdmin.js'
import { baseURL, headers } from '@/utils/utils'
onMounted(() => {
  getRoleList()
})
const getRoleList = () => {
  roleList({
    page: 1,
    per_page: 1000
  }).then((res) => {
    console.log(res)
    options.value = res.data.data
  })
}
const role_id = ref('')
const options = ref([])
const labelPosition = ref<FormProps['labelPosition']>('right')

const formLabelAlign = reactive({
  name: '',
  username: '',
  password: '',
  check_password: '',

})
const fileList = ref([])
const onSuccess = (res) => {
  console.log('上传成功', res)
  let path = res.data[0].path
  let fullpath = `${baseURL}${path}`
  let file = [
    {
      name: 'levelLogo',
      url: fullpath
    }
  ]
  fileList.value = file

}
const handleRemove: UploadProps['onRemove'] = (uploadFile, uploadFiles) => {
  console.log(uploadFile, uploadFiles)
  fileList.value = []
}

const submit = () => {
  if (formLabelAlign.name == '') {
    return ElMessage.error('请输入用户名')
  }
  if (formLabelAlign.username == '') {
    return ElMessage.error('请输入昵称')
  }

  if (fileList.value.length == 0) {
    return ElMessage.error('请上传头像')
  }
  if (formLabelAlign.password == '') {
    return ElMessage.error('请输入密码')
  }
  if (formLabelAlign.check_password == '') {
    return ElMessage.error('请确认密码')
  }
  adminAdd({
    name: formLabelAlign.name,
    username: formLabelAlign.username,
    password: formLabelAlign.password,
    check_password: formLabelAlign.check_password,
    avatar: fileList.value.length ? fileList.value[0].url : '',
    role_id: role_id.value
  }).then(() => {
    back()
  })
}
const reset = () => {
  formLabelAlign.name = ''
  formLabelAlign.username = ''
  formLabelAlign.password = ''
  formLabelAlign.check_password = ''
  fileList.value = []
  role_id.value = ''

}
const router = useRouter()
const back = () => {
  router.back()
}
</script>
<style scoped >
:deep(.el-upload--picture-card) {
  background-color: transparent;
  border: none;
  width: auto;
}

:deep(.el-input) {
  box-shadow: none;
}
</style>